using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Microsoft.Phone.Controls
{
	internal static class ItemsControlExtensions
	{
		public static T GetParentItemsControl<T>(DependencyObject element) where T : ItemsControl
		{
			DependencyObject parent = VisualTreeHelper.GetParent(element);
			while (!(parent is T) && parent != null)
			{
				parent = VisualTreeHelper.GetParent(parent);
			}
			return (T)((object)parent);
		}
		public static IList<WeakReference> GetItemsInViewPort(ItemsControl list)
		{
			IList<WeakReference> list2 = new List<WeakReference>();
			ScrollViewer scrollViewer = VisualTreeHelper.GetChild(list, 0) as ScrollViewer;
			list.UpdateLayout();
			if (scrollViewer == null)
			{
				return list2;
			}
			for (int i = 0; i < list.get_Items().get_Count(); i++)
			{
				FrameworkElement frameworkElement = (FrameworkElement)list.get_ItemContainerGenerator().ContainerFromIndex(i);
				if (frameworkElement != null)
				{
					GeneralTransform generalTransform = null;
					try
					{
						generalTransform = frameworkElement.TransformToVisual(scrollViewer);
					}
					catch (ArgumentException)
					{
						IList<WeakReference> result = list2;
						return result;
					}
					Rect rect = new Rect(generalTransform.Transform(default(Point)), generalTransform.Transform(new Point(frameworkElement.get_ActualWidth(), frameworkElement.get_ActualHeight())));
					if (rect.get_Bottom() <= 0.0)
					{
						goto IL_92;
					}
					list2.Add(new WeakReference(frameworkElement));
					i++;
					IL_131:
					while (i < list.get_Items().get_Count())
					{
						frameworkElement = (FrameworkElement)list.get_ItemContainerGenerator().ContainerFromIndex(i);
						generalTransform = null;
						try
						{
							generalTransform = frameworkElement.TransformToVisual(scrollViewer);
						}
						catch (ArgumentException)
						{
							IList<WeakReference> result = list2;
							return result;
						}
						rect = new Rect(generalTransform.Transform(default(Point)), generalTransform.Transform(new Point(frameworkElement.get_ActualWidth(), frameworkElement.get_ActualHeight())));
						if (rect.get_Top() >= scrollViewer.get_ActualHeight())
						{
							return list2;
						}
						list2.Add(new WeakReference(frameworkElement));
						i++;
					}
					return list2;
				}
				IL_92:;
			}
			goto IL_131;
		}
	}
}
